import 'package:faker/i18n/i18n.dart';
import 'package:faker/res/style.dart';
import 'package:faker/utils/toast_util.dart';
import 'package:flutter/material.dart';
import 'package:flutter_email_sender/flutter_email_sender.dart';
import 'package:keyboard_actions/keyboard_actions.dart';

class FeedbackScreen extends StatefulWidget {
  @override
  _FeedbackScreenState createState() => _FeedbackScreenState();
}

class _FeedbackScreenState extends State<FeedbackScreen> {
  FocusNode _nodeText1 = FocusNode();

  String feedBackMessage;

  @override
  void initState() {
    super.initState();
  }

  @override
  void dispose() {
    // Clean up the controller when the widget is disposed.
    super.dispose();
  }

  KeyboardActionsConfig _buildConfig(BuildContext context) {
    return KeyboardActionsConfig(
      keyboardActionsPlatform: KeyboardActionsPlatform.ALL,
      keyboardBarColor: Theme.of(context).backgroundColor,
      nextFocus: true,
      actions: [
        KeyboardAction(
          focusNode: _nodeText1,
          closeWidget: Padding(
            padding: EdgeInsets.all(8.0),
            child: Icon(Icons.close),
          ),
        ),
      ],
    );
  }

  @override
  Widget build(BuildContext context) {
    final ThemeData theme = Theme.of(context);

    return Container(
      color: theme.backgroundColor,
      child: SafeArea(
        top: false,
        child: Scaffold(
          backgroundColor: theme.backgroundColor,
          body: SingleChildScrollView(
            child: SizedBox(
              height: MediaQuery.of(context).size.height - 100,
              child: Column(
                children: <Widget>[
                  SizedBox(
                    height: 188,
                  ),
                  Container(
                    height: 180,
                    width: 320,
                    padding: EdgeInsets.only(
                        top: MediaQuery.of(context).padding.top,
                        left: 16,
                        right: 16),
                    decoration: BoxDecoration(
                      borderRadius:
                          new BorderRadius.all(new Radius.circular(5.0)),
                      image: DecorationImage(
                        image: AssetImage("assets/images/feedbackImage.jpg"),
                        fit: BoxFit.cover,
                      ),
                    ),
                  ),
                  Container(
                    padding: const EdgeInsets.only(top: 8),
                    child: Text(
                      S.of(context).youFeedBack,
                      style: TextStyle(
                        fontSize: 20,
                        fontWeight: FontWeight.bold,
                      ),
                    ),
                  ),
                  Container(
                    padding: const EdgeInsets.only(top: 16),
                    child: Text(
                      S.of(context).thkFeed,
                      textAlign: TextAlign.center,
                      style: TextStyle(
                        fontSize: 16,
                      ),
                    ),
                  ),
                  KeyboardActions(
                    config: _buildConfig(context),
                    autoScroll: false,
                    child: Padding(
                      padding:
                          const EdgeInsets.only(top: 16, left: 32, right: 32),
                      child: Container(
                        decoration: BoxDecoration(
                          color: theme.backgroundColor,
                          borderRadius: BorderRadius.circular(8),
                          boxShadow: <BoxShadow>[
                            BoxShadow(
                                color: Colors.grey.withOpacity(0.8),
                                offset: const Offset(4, 4),
                                blurRadius: 8),
                          ],
                        ),
                        child: ClipRRect(
                          borderRadius: BorderRadius.circular(25),
                          child: Container(
                            padding: const EdgeInsets.all(4.0),
                            constraints: const BoxConstraints(
                                minHeight: 80, maxHeight: 160),
                            color: theme.backgroundColor,
                            child: SingleChildScrollView(
                              padding: const EdgeInsets.only(
                                  left: 10, right: 10, top: 0, bottom: 0),
                              child: TextField(
                                maxLines: null,
                                onChanged: (String txt) {
                                  feedBackMessage = txt.trim();
                                },
                                focusNode: _nodeText1,
                                style: TextStyle(
                                  fontFamily: fontName,
                                  fontSize: 16,
                                  color: theme.textSelectionColor,
                                ),
                                cursorColor: Colors.blue,
                                decoration: InputDecoration(
                                    border: InputBorder.none,
                                    hintText: S.of(context).enterFeedBack),
                              ),
                            ),
                          ),
                        ),
                      ),
                    ),
                  ),
                  Padding(
                    padding: const EdgeInsets.only(top: 16),
                    child: Center(
                      child: Container(
                        width: 120,
                        height: 40,
                        decoration: BoxDecoration(
                          color: Colors.blue,
                          borderRadius:
                              const BorderRadius.all(Radius.circular(4.0)),
                          boxShadow: <BoxShadow>[
                            BoxShadow(
                                color: Colors.grey.withOpacity(0.6),
                                offset: const Offset(4, 4),
                                blurRadius: 8.0),
                          ],
                        ),
                        child: Material(
                          color: Colors.transparent,
                          child: InkWell(
                            onTap: () async {
                              FocusScope.of(context).requestFocus(FocusNode());
                              print(feedBackMessage);
                              if ("" == feedBackMessage ||
                                  null == feedBackMessage) {
                                T.show(msg: S.of(context).noFeedBackMessage);
                                return;
                              }
                              final Email email = Email(
                                body: feedBackMessage,
                                subject: 'RSS CLUB FEEDBACK',
                                recipients: ['taogezz@live.com'],
                                cc: ['yutaozhoucn@gmail.com'],
                                isHTML: false,
                              );
                              await FlutterEmailSender.send(email);
                            },
                            child: Center(
                              child: Padding(
                                padding: const EdgeInsets.all(4.0),
                                child: Text(
                                  S.of(context).send,
                                  style: TextStyle(
                                    fontWeight: FontWeight.w500,
                                    color: Colors.white,
                                  ),
                                ),
                              ),
                            ),
                          ),
                        ),
                      ),
                    ),
                  )
                ],
              ),
            ),
          ),
        ),
      ),
    );
  }
}
